package com.lagou.edu.service.impl;

import com.lagou.edu.dao.LagouTokenDao;
import com.lagou.edu.dao.UserDao;
import com.lagou.edu.enums.CodeStatus;
import com.lagou.edu.pojo.LagouToken;

import com.lagou.edu.pojo.User;
import com.lagou.edu.service.CodeService;
import com.lagou.edu.service.UserService;

import com.lagou.edu.utils.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
import java.util.UUID;


/**
 * @Author grung
 * @Date 2020/10/23 13:46
 * @Version 1.0
 */

@Service
@Component
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;

    @Autowired
    private LagouTokenDao lagouTokenDao;

    @Reference
    private CodeService codeService;


    private static String SESSION_ID = "lagouSessionId";

    @Override
    public Result login(String email, String password, HttpServletResponse response) {

        if (!isRegistered(email)) {
            return Result.error("该用户尚未注册");
        }

        User user = userDao.findUserByEmailAndPassword(email, password);

        if (user == null) {
            return Result.error("邮箱或者密码错误");
        }


        //UUID生成token
        String token = UUID.randomUUID().toString();

        LagouToken lagouToken = lagouTokenDao.findByEmail(email);
        if (lagouToken == null) {
            lagouToken = new LagouToken();
            lagouToken.setEmail(email);
            lagouToken.setToken(token);
            lagouTokenDao.save(lagouToken);

            //更新cookie和token
            Cookie cookie = new Cookie(SESSION_ID, token);

            cookie.setPath("/");
            response.addCookie(cookie);
        }else{
            //更新cookie和token
            Cookie cookie = new Cookie(SESSION_ID, lagouToken.getToken());

            cookie.setPath("/");
            response.addCookie(cookie);
        }

        return Result.ok(email);

    }

    @Override
    public Boolean isRegistered(String email) {
        User user = userDao.findByEmail(email);
        if (user != null) {
            return true;
        }
        return false;
    }

    @Override
    public Result register(String email, String password, String code,HttpServletResponse response) {

        if(isRegistered(email)){
            return Result.error("用户已注册，请直接登录");
        }
        Integer validateCode = codeService.vaildate(email, code);
        //验证码正确
        if(Objects.equals(validateCode, CodeStatus.RIGHT.getCode())){
            //存储用户信息
            User user = new User();
            user.setEmail(email);
            user.setPassword(password);
            userDao.save(user);
            //存储token信息，因为注册成功后会直接调整到欢迎页，需要生产一下token
            String token = UUID.randomUUID().toString();
            LagouToken lagouToken = new LagouToken();
            lagouToken.setEmail(email);
            lagouToken.setToken(token);
            lagouTokenDao.save(lagouToken);
            Cookie cookie = new Cookie(SESSION_ID,token);
            cookie.setPath("/");
            response.addCookie(cookie);
            return Result.ok();
        }else{
            String msg = CodeStatus.getDesc(validateCode);
            return Result.error(msg);
        }
    }

    @Override
    public Result info(String token) {

        if (token!=null){
            LagouToken byToken = lagouTokenDao.findByToken(token);
            if (byToken!=null){
                return Result.ok(byToken.getEmail());
            }
        }


        return Result.error("非法操作!");
    }


}
